package com.robot.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.robot.dao.ProjectMapper;
import com.robot.model.Project;
import com.robot.model.ProjectMember;
import com.robot.model.User;
import com.robot.util.SessionUtil;
import com.robot.vo.ResponseMessage;
import com.robot.vo.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/6/16
 * Time: 16:57
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Service
@Slf4j
public class ProjectService {

    @Autowired
    ProjectMapper projectMapper;

    /**
     * 修改项目
     * **/
    public ResponseMessage modifyProject(Project project){
        try {
            int i = projectMapper.updateProject(project);
            if(i !=1 ){
                return ResponseMessage.errorResponse("新增项目异常,请联系管理员");
            }
            return ResponseMessage.successResponse();
        }catch (Exception e){
            log.error("修改项目异常",e);
            return ResponseMessage.errorResponse("修改项目异常,请联系管理员");
        }
    }


    /**
     * 新增项目
     * @param project
     * **/
    public ResponseMessage addProject(Project project,Integer userId){
        try{
            int i = projectMapper.addProject(project);
            if(i !=1 ){
                return ResponseMessage.errorResponse("新增项目异常,请联系管理员");
            }
        }catch (Exception e){
            log.error("新增项目异常,请联系管理员",e);
            return ResponseMessage.errorResponse("新增项目异常,请联系管理员");
        }
        ProjectMember projectMember = new ProjectMember();
        projectMember.setProjectId(project.getId());
        projectMember.setUserId(userId);
        return this.addProjectMember(projectMember);
    }
    /**
     * 查询项目
     * @param project
     * **/
    public ResponseMessage getProjectList(Project project){
//        第一点 参数是需要有用户
        try {
            User userDo =(User) SessionUtil.getSessionUser();
            Integer userId= userDo.getId();
//            第三个参数代表是否需要返回总行数
            Map map=new HashMap();
            map.put("userId",userId);
            map.put("projectName",project.getProjectName());
            Page page =PageHelper.startPage(project.getPageNum(),project.getPageSize(),true);
            //这里出现异常，导致后面的sql没有被执行，那么上面那段page代码状态就是未执行，当其他地方执行sql的时候，会去执行
            List<Project> projectList = projectMapper.getProjectList(map);
            //1.你要返回当前查询出来的数据
            //2.你要返回当前条件数据的总行数，前端框架才能出现下一页、总行数等信息
            Long total =  page.getTotal();
            TablePage tablePage=new TablePage();
            tablePage.setTotal(total);
            tablePage.setRows(projectList);
            return ResponseMessage.successResponse(tablePage);
        }catch (Exception e){
            log.error("查询项目列表异常",e);
            return ResponseMessage.errorResponse("查询项目列表异常");
        }
    }
    /**
     * 添加项目成员
     * @param projectMember
     * **/
    public ResponseMessage addProjectMember(ProjectMember projectMember){
        try{
            int i = projectMapper.addProjectMember(projectMember);
            if(i !=1 ){
                return ResponseMessage.errorResponse("新增项目成员异常,请联系管理员");
            }
        }catch (Exception e){
            log.error("新增项目成员异常,请联系管理员",e);
            return ResponseMessage.errorResponse("新增项目成员异常,请联系管理员");
        }
        return ResponseMessage.successResponse();
    }
    /**
     * 移出项目成员
     * @param projectMember
     * **/
    public ResponseMessage removeUserFromProject(ProjectMember projectMember){
        try{
            int i = projectMapper.removeUserFromProject(projectMember);
            if(i !=1 ){
                return ResponseMessage.errorResponse("移出项目成员异常,请联系管理员");
            }
        }catch (Exception e){
            log.error("移出项目成员异常,请联系管理员",e);
            return ResponseMessage.errorResponse("移出项目成员异常,请联系管理员");
        }
        return ResponseMessage.successResponse();
    }
    /**
     * 获取项目成员列表
     * **/
    public ResponseMessage getProjectMemberList(ProjectMember projectMember){
        try{
            Page page=PageHelper.startPage(projectMember.getPageNum(),projectMember.getPageSize(),true);
            List<User> userList=projectMapper.getProjectMemberList(projectMember);
            Long total =page.getTotal();
            TablePage tablePage=new TablePage();
            tablePage.setTotal(total);
            tablePage.setRows(userList);
            return ResponseMessage.successResponse(tablePage);
        }catch (Exception e){
            log.error("获取项目成员异常",e);
            return ResponseMessage.errorResponse("获取项目成员异常");
        }
    }
    /**
     * 初始化项目，在用户注册成功之后创建一个项目
     * @param user
     * **/
    public void initProject(User user){
        Project project=new Project();
        project.setProjectName(user.getUserName()+"的默认项目");
        this.addProject(project,user.getId());
    }

}
